home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sh-utils.12 / sh-utils / sh-utils-1.12 / src / uname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  4.6 KB  |  203 lines

  1. /* uname -- print system information
  2.    Copyright (C) 89, 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Option        Example
  19.  
  20.    -s, --sysname    SunOS
  21.    -n, --nodename    rocky8
  22.    -r, --release    4.0
  23.    -v, --version    
  24.    -m, --machine    sun
  25.    -a, --all        SunOS rocky8 4.0  sun
  26.  
  27.    The default behavior is equivalent to `-s'.
  28.  
  29.    David MacKenzie <djm@gnu.ai.mit.edu> */
  30.  
  31. #include <config.h>
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include <sys/utsname.h>
  35. #include <getopt.h>
  36.  
  37. #include "system.h"
  38. #include "version.h"
  39.  
  40. void error ();
  41.  
  42. static void print_element ();
  43. static void usage ();
  44.  
  45. /* Values that are bitwise or'd into `toprint'. */
  46. /* Operating system name. */
  47. #define PRINT_SYSNAME 1
  48.  
  49. /* Node name on a communications network. */
  50. #define PRINT_NODENAME 2
  51.  
  52. /* Operating system release. */
  53. #define PRINT_RELEASE 4
  54.  
  55. /* Operating system version. */
  56. #define PRINT_VERSION 8
  57.  
  58. /* Machine hardware name. */
  59. #define PRINT_MACHINE 16
  60.  
  61. /* Mask indicating which elements of the name to print. */
  62. static unsigned char toprint;
  63.  
  64. /* The name this program was run with, for error messages. */
  65. char *program_name;
  66.  
  67. /* If non-zero, display usage information and exit.  */
  68. static int show_help;
  69.  
  70. /* If non-zero, print the version on standard output and exit.  */
  71. static int show_version;
  72.  
  73. static struct option const long_options[] =
  74. {
  75.   {"help", no_argument, &show_help, 1},
  76.   {"machine", no_argument, NULL, 'm'},
  77.   {"nodename", no_argument, NULL, 'n'},
  78.   {"release", no_argument, NULL, 'r'},
  79.   {"sysname", no_argument, NULL, 's'},
  80.   {"version", no_argument, &show_version, 1},
  81.   {"all", no_argument, NULL, 'a'},
  82.   {NULL, 0, NULL, 0}
  83. };
  84.  
  85. void
  86. main (argc, argv)
  87.      int argc;
  88.      char **argv;
  89. {
  90.   struct utsname name;
  91.   int c;
  92.  
  93.   program_name = argv[0];
  94.   toprint = 0;
  95.  
  96.   while ((c = getopt_long (argc, argv, "snrvma", long_options, (int *) 0))
  97.      != EOF)
  98.     {
  99.       switch (c)
  100.     {
  101.     case 0:
  102.       break;
  103.  
  104.     case 's':
  105.       toprint |= PRINT_SYSNAME;
  106.       break;
  107.  
  108.     case 'n':
  109.       toprint |= PRINT_NODENAME;
  110.       break;
  111.  
  112.     case 'r':
  113.       toprint |= PRINT_RELEASE;
  114.       break;
  115.  
  116.     case 'v':
  117.       toprint |= PRINT_VERSION;
  118.       break;
  119.  
  120.     case 'm':
  121.       toprint |= PRINT_MACHINE;
  122.       break;
  123.  
  124.     case 'a':
  125.       toprint = PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
  126.         PRINT_VERSION | PRINT_MACHINE;
  127.       break;
  128.  
  129.     default:
  130.       usage (1);
  131.     }
  132.     }
  133.  
  134.   if (show_version)
  135.     {
  136.       printf ("uname - %s\n", version_string);
  137.       exit (0);
  138.     }
  139.  
  140.   if (show_help)
  141.     usage (0);
  142.  
  143.   if (optind != argc)
  144.     usage (1);
  145.  
  146.   if (toprint == 0)
  147.     toprint = PRINT_SYSNAME;
  148.  
  149.   if (uname (&name) == -1)
  150.     error (1, errno, "cannot get system name");
  151.  
  152.   print_element (PRINT_SYSNAME, name.sysname);
  153.   print_element (PRINT_NODENAME, name.nodename);
  154.   print_element (PRINT_RELEASE, name.release);
  155.   print_element (PRINT_VERSION, name.version);
  156.   print_element (PRINT_MACHINE, name.machine);
  157.  
  158.   exit (0);
  159. }
  160.  
  161. /* If the name element set in MASK is selected for printing in `toprint',
  162.    print ELEMENT; then print a space unless it is the last element to
  163.    be printed, in which case print a newline. */
  164.  
  165. static void
  166. print_element (mask, element)
  167.      unsigned char mask;
  168.      char *element;
  169. {
  170.   if (toprint & mask)
  171.     {
  172.       toprint &= ~mask;
  173.       printf ("%s%c", element, toprint ? ' ' : '\n');
  174.     }
  175. }
  176.  
  177. static void
  178. usage (status)
  179.      int status;
  180. {
  181.   if (status != 0)
  182.     fprintf (stderr, "Try `%s --help' for more information.\n",
  183.          program_name);
  184.   else
  185.     {
  186.       printf ("Usage: %s [OPTION]...\n", program_name);
  187.       printf ("\
  188. \n\
  189.   -a, --all        print all information\n\
  190.   -m, --machine    print the machine (hardware) type\n\
  191.   -n, --nodename   print the machine's network node hostname\n\
  192.   -r, --release    print the operating system release\n\
  193.   -s, --sysname    print the operating system name\n\
  194.   -v               print the operating system version\n\
  195.       --help       display this help and exit\n\
  196.       --version    output version information and exit\n\
  197. \n\
  198. Without any OPTION, assume -s.\n\
  199. ");
  200.     }
  201.   exit (status);
  202. }
  203.